const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const {DefinePlugin} = require('webpack');
const fs = require('fs');

// Read the project information from package.json
const packageJson = JSON.parse(fs.readFileSync(path.resolve('./package.json'), 'utf8'));
const projectName = `"${packageJson.name}"`
const projectVersion = `"${packageJson.version}"`

module.exports = {
    entry: path.resolve(__dirname, 'entry/index.js'),
    output: {
        path: path.resolve('./dist/'),
        filename: 'index.js',
        library: {
            name: projectName,
            type: 'umd',
            umdNamedDefine: true,
        },
    },
    resolve: {
        alias: {
            '@': path.resolve('.'),
            '@dist': path.resolve('./dist'),
            '@src': path.resolve('./src'),
            '@@': path.resolve('./src'),
            '@@main': path.resolve('./src/main'),
            '@@serve': path.resolve('./src/serve'),
            '@@test': path.resolve('./src/test'),
        },
        extensions: ['.js', '.vue'],
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env'],
                        plugins: ['@babel/transform-runtime'],
                    },
                },
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader',
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                ],
            },
        ],
    },
    mode: 'production',
    plugins: [
        new VueLoaderPlugin(),
        new DefinePlugin({
            'project.name': projectName,
            'project.version': projectVersion,
        }),
    ],
};
